package com.sk.waste.app.adapters

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.lc.smc.lib.interfaces.LCInterface
import com.sk.waste.app.R
import com.sk.waste.app.models.MaterialModel
import com.sk.waste.app.models.SubMaterialModel
import com.sk.waste.app.models.TypeModel

/**
 *  @time 2025/01/07  15:37
 *  @desc 分类-小类
 */
class TypeAdapter(val context: Context, val list: List<SubMaterialModel>, val callback: LCInterface.LcCallBack?) :
    RecyclerView.Adapter<TypeAdapter.ViewHolder>() {


    override fun getItemCount(): Int {
        return list.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TypeAdapter.ViewHolder {
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.adapter_type, parent, false))
    }

    @SuppressLint("NotifyDataSetChanged", "SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, @SuppressLint("RecyclerView") position: Int) {

        val item = list[position]
        holder.ttvName.text = item.subcategoryName
        if (item.isChecked) {
            holder.ttvName.setTextColor(context.getColor(com.lc.smc.lib.R.color.color_green))
            holder.imgChecked.visibility = View.VISIBLE
            holder.rootView.setBackgroundResource(R.color.color_content)
        } else {
            holder.ttvName.setTextColor(context.getColor(com.lc.smc.lib.R.color.color_text_gray_g))
            holder.imgChecked.visibility = View.INVISIBLE
            holder.rootView.setBackgroundResource(R.color.color_transparent)
        }

        holder.rootView.tag = position
        holder.rootView.setOnClickListener {
            val currentSelect = holder.rootView.tag as Int
            clearChecked()
            list[currentSelect].isChecked = true
            notifyItemChanged(currentSelect)
            callback?.callBack(holder.rootView.tag, -1)
        }

    }

    private fun clearChecked() {
        for (item in list) {
            item.isChecked = false
        }
        notifyDataSetChanged()
    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val ttvName: TextView
        val imgChecked: ImageView
        val rootView: LinearLayout

        init {
            ttvName = view.findViewById(R.id.item_type_name)
            imgChecked = view.findViewById(R.id.item_type_check)
            rootView = view.findViewById(R.id.item_root_layout)
        }
    }


}